package com.dupengcheng.subject.application.controller;

import com.alibaba.fastjson.JSONObject;
import com.dupengcheng.subject.application.convert.SubjectLabelDTOConvert;

import com.dupengcheng.subject.application.dto.SubjectLabelDTO;
import com.dupengcheng.subject.common.entity.Result;
import com.dupengcheng.subject.domain.entity.SubjectLabelBO;
import com.dupengcheng.subject.domain.service.SubjectLabelDomainService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/subject/label")
public class SubjectLabelController {
    @Resource
    private SubjectLabelDomainService labelDomainService;


    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectLabelDTO subjectLabelDTO){

        try{
            if (log.isInfoEnabled()){
                log.info("SubjectLabelController.add.dto:{}",
                        JSONObject.toJSONString(subjectLabelDTO));
            }
            Preconditions.checkNotNull(subjectLabelDTO.getLabelName(),"标签名称不能为空");
            SubjectLabelBO subjectLabelBO= SubjectLabelDTOConvert.INSTANCE
                    .convertDTOtoBO(subjectLabelDTO);
            boolean add = labelDomainService.add(subjectLabelBO);
            return Result.ok(add);

        }catch (Exception e){
            log.error("SubjectLabelController.add.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectLabelDTO subjectLabelDTO){
        try{
            if (log.isInfoEnabled()){
                log.info("SubjectLabelController.update.dto:{}",
                        JSONObject.toJSONString(subjectLabelDTO));
            }
            Preconditions.checkNotNull(subjectLabelDTO.getId(),"ID不能为空！");
            SubjectLabelBO subjectLabelBO= SubjectLabelDTOConvert.INSTANCE
                    .convertDTOtoBO(subjectLabelDTO);
            boolean add = labelDomainService.update(subjectLabelBO);
            return Result.ok(add);

        }catch (Exception e){
            log.error("SubjectLabelController.add.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }


    }
    @PostMapping("/delete")
    public Result<Boolean> del(@RequestBody SubjectLabelDTO subjectLabelDTO){

        try{
            if (log.isInfoEnabled()){
                log.info("SubjectLabelController.del.dto:{}",
                        JSONObject.toJSONString(subjectLabelDTO));
            }
            Preconditions.checkNotNull(subjectLabelDTO.getId(),"ID不能为空！");
            SubjectLabelBO subjectLabelBO= SubjectLabelDTOConvert.INSTANCE
                    .convertDTOtoBO(subjectLabelDTO);
            boolean add = labelDomainService.delete(subjectLabelBO);
            return Result.ok(add);

        }catch (Exception e){
            log.error("SubjectLabelController.add.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }



    }

    @PostMapping("/queryByCategory")
    public Result<List<SubjectLabelDTO>> queryByCategory(@RequestBody SubjectLabelDTO subjectLabelDTO){
        try{
            if (log.isInfoEnabled()){
                log.info("SubjectLabelController.del.dto:{}",
                        JSONObject.toJSONString(subjectLabelDTO));
            }
            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(),"分类ID不能为空！");
            SubjectLabelBO subjectLabelBO= SubjectLabelDTOConvert.INSTANCE
                    .convertDTOtoBO(subjectLabelDTO);
            List<SubjectLabelBO> subjectLabelBOS = labelDomainService.query(subjectLabelBO);
            List<SubjectLabelDTO> subjectLabelDTOS=SubjectLabelDTOConvert.INSTANCE.convertBOtoDTOList(subjectLabelBOS);
            return Result.ok(subjectLabelDTOS);

        }catch (Exception e){
            log.error("SubjectLabelController.add.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }



    }

}
